package com.cdelink.xzgy.ftp.util;



import com.cdelink.xzgy.ftp.exception.InvalidDataException;
import org.apache.commons.lang3.StringUtils;

import java.util.Random;

public class CommonUtil {
    /**
     * 检查对象是否为null
     *
     * @param obj
     */
    public static void checkObject(Object obj) {
        if (null == obj) {
            throw new InvalidDataException("对象不能为空");
        }
    }

    public static void checkStrParam(String param, String desc) {
        if (StringUtils.isBlank(param)) {
            throw new InvalidDataException(desc + "不能为空");
        }
    }

    public static void checkIdParam(Integer param, String desc) {
        if (null == param) {
            throw new InvalidDataException(desc + "不能为空");
        }
    }


    /**
     * 生成随机验证码
     *
     * @param num 位数
     * @return
     */
    public static String generateCode(int num) {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            //type 1 (A->Z) type 2 (a->z) type 3 (0->9)
            int type = random.nextInt(3) + 1;
            switch (type) {
                case 1:
                    char codeOne = (char) (random.nextInt(26) + 65);
                    code = code + codeOne;
                    break;
                case 2:
                    char codeTwo = (char) (random.nextInt(26) + 97);
                    code = code + codeTwo;
                    break;
                case 3:
                    int codeThree = random.nextInt(10);
                    code = code + codeThree;
                    break;
                default:
                    break;
            }
        }
        return code;
    }

}
